/**
 *  *2010-3-11 下午10:00:03  add 
 */
package com.base.myproject.client.busi.ccode;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseGrid;
import com.extjs.gxt.ui.client.Style.Direction;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.fx.FxConfig;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Anchor;

/**
 * @author Administrator 2010-3-11 javazgw@gmail.com
 */
public class CcodeLinkMan extends LayoutContainer implements ClickHandler {
	final String title = "联系人信息";
	ContentPanel mainpanel = new ContentPanel();
	boolean isdetailvisible = false; // 内容只能被增加一次
	BaseStore<BusiObject> presentstore;
	BaseStore<BusiObject> store;
	BaseGrid<BusiObject> grid;
	int height = 150;
	int width = 620;

	public CcodeLinkMan(BaseStore<BusiObject> presentstore) {
		Anchor anchor = new Anchor(title);
		anchor.addClickHandler(this);
		this.setPresentstore(presentstore);
		this.add(anchor);
		this.add(mainpanel);
		mainpanel.setVisible(false);
		mainpanel.setBorders(true);
		mainpanel.setScrollMode(Scroll.AUTO);
	}

	@Override
	public void onClick(ClickEvent event) {
		initCCodeMain();
		if (mainpanel.isVisible()) {

			mainpanel.el().slideOut(Direction.UP, FxConfig.NONE);
		} else {

			mainpanel.setVisible(true);
			mainpanel.el().slideIn(Direction.DOWN, FxConfig.NONE);
		}

	}

	public void initCCodeMain() {
		if (!isdetailvisible) {
			// mainpanel.add(new Button("ss"));
			// mainpanel.layout();
			initInnerWidget();
			isdetailvisible = true;
		}
	}

	private void initInnerWidget() {
		// ServiceItemPanel tb3 = new ServiceItemPanel();
		getStore();

		// mainpanel.add(grid);
		mainpanel.add(createGrid());
		mainpanel.setWidth(width);
		mainpanel.layout();
	}

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("ccode");
		type.getField("ccode").setType(String.class);
		type.addField("lname");
		type.getField("lname").setType(String.class);
		type.addField("dept");
		type.getField("dept").setType(String.class);
		type.addField("headship");
		type.getField("headship").setType(String.class);
		type.addField("postcode");
		type.getField("postcode").setType(String.class);
		type.addField("email");
		type.getField("email").setType(String.class);
		type.addField("telephone");
		type.getField("telephone").setType(String.class);
		type.addField("mobil");
		type.getField("mobil").setType(String.class);
		type.addField("fax");
		type.getField("fax").setType(String.class);
		type.addField("passportcode");
		type.getField("passportcode").setType(String.class);
		type.addField("privatemobil");
		type.getField("privatemobil").setType(String.class);
		type.addField("addr");
		type.getField("addr").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("ccodelinkman_info");
		info.setForcenolimit("true");
		info.setKeyname(new String[] { "icode" });
		
		info
				.setSql("select icode,ccode,lname,dept,headship,postcode,email,telephone,mobil,fax,passportcode,privatemobil,addr from ccodelinkman_info where ccode='{ccode}'");
		 info.setParent(presentstore);// 关联主表
		 info.setLinkparentcolumns(new String[] { "ccode" }); // 关联主表字段
		 info.setLinkparent_chile_columns(new String[] { "ccode" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info);
		return store;
	}

	BaseGrid createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

//		ColumnConfig C1 = new ColumnConfig("icode", "icode", 80);
//		columns.add(C1);
//		ColumnConfig C2 = new ColumnConfig("ccode", "ccode", 80);
//		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("lname", "姓名", 180);
		C3.setEditor( new CellEditor(new TextField<String>()));
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("dept", "所在部门", 180);
		C4.setEditor( new CellEditor(new TextField<String>()));
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("headship", "职务", 120);
		C5.setEditor( new CellEditor(new TextField<String>()));
		columns.add(C5);
		ColumnConfig C6 = new ColumnConfig("postcode", "邮编", 120);
		C6.setEditor( new CellEditor(new TextField<String>()));
		columns.add(C6);
		ColumnConfig C7 = new ColumnConfig("email", "email", 180);
		C7.setEditor( new CellEditor(new TextField<String>()));
		columns.add(C7);
		ColumnConfig C8 = new ColumnConfig("telephone", "电话", 180);
		C8.setEditor( new CellEditor(new TextField<String>()));
		columns.add(C8);
		ColumnConfig C9 = new ColumnConfig("mobil", "手机", 180);
		C9.setEditor( new CellEditor(new TextField<String>()));
		columns.add(C9);
		ColumnConfig C10 = new ColumnConfig("fax", "fax",180);
		C10.setEditor( new CellEditor(new TextField<String>()));
		columns.add(C10);
//		ColumnConfig C11 = new ColumnConfig("passportcode", "passportcode", 80);
//		columns.add(C11);
//		ColumnConfig C12 = new ColumnConfig("privatemobil", "privatemobil", 80);
//		columns.add(C12);
		ColumnConfig C13 = new ColumnConfig("addr", "通信地址", 280);
		C13.setEditor( new CellEditor(new TextField<String>()));
		columns.add(C13);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid(getStore(), cm);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setHeight(height);
		grid.setWidth(800);
		//grid.setAutoWidth(true);
		grid.getView().setForceFit(true);
		return grid;
	}

	public BaseStore<BusiObject> getPresentstore() {
		return presentstore;
	}

	public void setPresentstore(BaseStore<BusiObject> presentstore) {
		this.presentstore = presentstore;
	}
	
}
